//
//  LXJEmoticonKeyBoardToolBar.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/7.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 表情键盘的工具条

enum EmoticonKeyBoardToolBarButtonType: Int {
    /// 最近
    case recent = 1
    /// 默认
    case normal
    /// Emoji
    case emoji
    /// 浪小花
    case langxiaohua
}
@available(iOS 9.0, *)
class LXJEmoticonKeyBoardToolBar: UIStackView {
    // MARK: 属性 
    ///用于记录上次选中的摁扭
    fileprivate var lastSelectedButton: UIButton?
    
    /// 点击摁扭的闭包
    var buttonClickClosure: ((EmoticonKeyBoardToolBarButtonType) -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 设置水平方向填充
        self.axis = .horizontal
        /// 设置等比填充
        self.distribution = .fillEqually
        
        /// 添加子控件
        addChildButton(title: "最近", imageName: "compose_emotion_table_left",type: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid", type: .normal)
        addChildButton(title: "Emoji", imageName: "compose_emotion_table_mid", type: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right", type: .langxiaohua)
    }
    
    // MARK: 添加子控件
    fileprivate func addChildButton(title: String, imageName: String, type: EmoticonKeyBoardToolBarButtonType) -> Void {
        let button = UIButton()
        /// 设置摁扭的 tag 值
        button.tag = type.rawValue
        
        /// 设置文字
        button.setTitle(title, for: .normal)
        
        /// 设置文字颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        
        /// 设置字号大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        /// 设置背景图片
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), for: .selected)
        
        /// 取消高亮效果
        button.adjustsImageWhenHighlighted = false
        
        /// 监听摁扭的点击事件
        button.addTarget(self, action: #selector(toolBarButtonClickAction(sender:)), for: .touchUpInside)
        
        /// 设置默认表情摁的选中状态
        if type == .normal {
            /// 取消上次选中
            lastSelectedButton?.isSelected = false
            /// 默认摁扭选中
            button.isSelected = true
            /// 赋值给记录的上次摁扭
            lastSelectedButton = button
        }
        
        /// 添加到父控件身上
        addArrangedSubview(button)
    }
    
    /// 摁扭的点击事件
    @objc fileprivate func toolBarButtonClickAction(sender: UIButton) -> Void {
        /// 如果这次的点击事件和上次的摁扭一样 不予执行
        if lastSelectedButton == sender {
            return
        }
        
        /// 将上一个选中的摁扭的选中状态进行取消
        lastSelectedButton?.isSelected = false
        /// 将本次摁扭选中
        sender.isSelected = true
        /// 给上次摁扭赋值
        lastSelectedButton = sender
        
        /// 执行闭包
        buttonClickClosure?(EmoticonKeyBoardToolBarButtonType(rawValue: sender.tag)!)
    }
    
    /// 根据下标选中摁扭
    func selectedButton(indexPath: IndexPath) -> Void {
        /// 根据 tag 值获取对应的 button
        let button = viewWithTag(indexPath.section + 1) as! UIButton
        /// 如果这个摁扭跟上次的摁扭不一致的时候
        if button == lastSelectedButton {
            return
        }
        
        /// 取消上次的选中摁扭
        lastSelectedButton?.isSelected = false
        /// 本次摁扭选中
        button.isSelected = true
        /// 赋值上次的摁扭以记录
        lastSelectedButton = button
    }
}
